﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

/// <summary>
/// uGUI事件转发
/// </summary>
public class EventForward : MonoBehaviour, IPointerClickHandler //, IPointerDownHandler, IPointerUpHandler
{
    /// <summary>
    /// 接收转发事件的对象
    /// </summary>
    public GameObject target;

    /// <summary>
    /// 点击了某个区域
    /// </summary>
    public UnityEvent OnAreaClick;

    // /// <summary>
    // /// 点击了某个区域
    // /// </summary>
    // public UnityEvent OnAreaUp;

    // /// <summary>
    // /// 点击了某个区域
    // /// </summary>
    // public UnityEvent OnAreaDown;

    /// <summary>
    /// 任意事件执行成功后执行的回调
    /// </summary>
    public Action SuccessAction;

    private void _DoSuccess()
    {
        if (null != SuccessAction)
        {
            SuccessAction();
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (null == target) return;
        // Target上有能处理当前事件的函数才会返回true
        if (ExecuteEvents.Execute(target, eventData, ExecuteEvents.pointerClickHandler))
        {
            _DoSuccess();
            OnAreaClick.Invoke();
        }
    }

    // public void OnPointerDown(PointerEventData eventData)
    // {
    //     print("touch");
    //     if (null == target) return;
    //     // Target上有能处理当前事件的函数才会返回true
    //     if (ExecuteEvents.Execute(target, eventData, ExecuteEvents.pointerDownHandler))
    //     {
    //         _DoSuccess();
    //         OnAreaDown.Invoke();
    //     }
    // }

    // public void OnPointerUp(PointerEventData eventData)
    // {
    //     print("touch");
    //     if (null == target) return;
    //     // Target上有能处理当前事件的函数才会返回true
    //     if (ExecuteEvents.Execute(target, eventData, ExecuteEvents.pointerUpHandler))
    //     {
    //         _DoSuccess();
    //         OnAreaUp.Invoke();
    //     }
    // }
}
